package com.example.onlineeducation.ui.common

import android.content.Context
import android.content.Intent
import android.view.View
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.databinding.NoticeDetailActivityBinding
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.extension.ycLoadImageNet

/**
 * SimpleDes:
 * Creator: Sindi
 * Date: 2022-05-10
 * UseDes:
 */
class NoticeDetailActivity : BaseActivity<NoticeDetailActivityBinding>(NoticeDetailActivityBinding::inflate) {

    companion object {
        private const val KEY_TITLE = "key_title"
        private const val KEY_CONTENT = "key_content"
        private const val KEY_IMG_URL = "key_img_url"
        private const val KEY_SHOW_IMG = "key_show_img"

        fun newInstance(context: Context, title: String?, showImg: Boolean, content: String? = null, imgUrl: String? = null) {
            val intent = Intent(context, NoticeDetailActivity::class.java).apply {
                putExtra(KEY_TITLE, title)
                putExtra(KEY_CONTENT, content)
                putExtra(KEY_IMG_URL, imgUrl)
                putExtra(KEY_SHOW_IMG, showImg)
            }
            context.startActivity(intent)
        }
    }

    override fun NoticeDetailActivityBinding.initView() {
        intent.apply {
            actionbarLayout.setTitle("公告")
            tvTitle.text = getStringExtra(KEY_TITLE) ?: "-"
            if (getBooleanExtra(KEY_SHOW_IMG, false)) {
                tvContent.visibility = View.GONE
                val imgUrl = getStringExtra(KEY_IMG_URL)
                if (imgUrl.ycIsNotEmpty()) {
                    ivImage.ycLoadImageNet(imgUrl, errorImg = R.drawable.ic_topic)
                    ivImage.visibility = View.VISIBLE
                } else {
                    ivImage.visibility = View.GONE
                }
            } else {
                tvContent.text = getStringExtra(KEY_CONTENT)
                tvContent.visibility = View.VISIBLE
                ivImage.visibility = View.GONE
            }
        }
    }
}